package handler

import (
	"github.com/gin-gonic/gin"
	"micros/api/params"
	"micros/golbal"
	"micros/model"
	"net/http"
)

func Moment(c *gin.Context) {

	met := params.AddMoments{}
	if err := c.ShouldBind(&met); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	moments := model.Moments{

		UserId:  met.UserId,
		Image:   met.Image,
		Content: met.Content,
		Count:   met.Count,
	}

	golbal.DB.Debug().Create(&moments)

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "朋友圈发布成功",
	})
}
func MomentList(c *gin.Context) {
	list := params.MomentList{}
	if err := c.ShouldBind(&list); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	var user []model.MomentList

	golbal.DB.Model(&model.User{}).Select("users.*,moments.friend_id").
		Joins("join moments on moments.user_id=users.id").Where("moments.friend_id").
		Where("moments.friend_id", list.FriendId).Scan(&user)

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "朋友圈列表查询成功",
		"data": user,
	})
}
